<?php

namespace App\Admin\Traits;

use App\Admin\Models\GameOperateLog;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

trait RestfulResponse
{
    /**
     * 返回 201 已创建 响应
     *
     * @param null|string|array $data
     * @param array $headers
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function created($data = null, array $headers = [])
    {
        return response()->json($data, 201, $headers);
    }

    /**
     * 返回 204 无内容 响应
     *
     * @param array $headers
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function noContent(array $headers = [])
    {
        return response()->json(null, 204, $headers);
    }


    /**
     * 返回 900 无内容 响应
     *
     * @param array $headers
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function errorMessage($data)
    {
        return response()->json($data, 202, []);
    }

    /**
     * 返回 200 OK 响应
     *
     * @param mixed $data
     * @param array $headers
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function ok($data = null, array $headers = [])
    {
        if ($data instanceof JsonResource) {
            return $data->response()->withHeaders($headers)->setStatusCode(200);
        } else {
            return response()->json($data, 200, $headers);
        }
    }

    /**
     * 返回 200 OK 响应
     *
     * @param mixed $data
     * @param array $headers
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function adminok($data = null, array $headers = [],$remark,$app_id,$type=1)
    {
        $this->insertActionLog($app_id,$remark,$type);
        if ($data instanceof JsonResource) {
            return $data->response()->withHeaders($headers)->setStatusCode(200);
        } else {
            return response()->json($data, 200, $headers);
        }
    }
    /**
     * @param Request $request
     * @param $params
     * @param $app_id
     * @param string $remark
     * @param int $type
     * 插入操作记录
     */

    public function insertActionLog($app_id,$remark = '新增游戏', $type = 1)
    {
        $request = [
            'method'=>\request()->method(),
            'url'=>\request()->url(),
            'ip'=>\request()->ip()
        ];
        $authData = auth('admin');
        $userData = $authData->user();
//        $param =$request['method'].':';
//        if(is_array($params)) {
//            foreach ($params as $key => &$value) {
//                if (is_array($value)) {
//                    $value = implode(',', $value);
//                }
//                $param .= $key . '=' . $value . '&';
//            }
//            $param = rtrim($param, '&');
//        }else{
//            $param .= $params;
//        }
        $insert = [
            'user_id' => $userData['id'],
            'username' => $userData['username'],
            'app_id' => $app_id,
            'action' => $request['url'],
            'remark'=>$remark,
            'param' => $request['method'].':'.http_build_query(\request()->input()),
            'ip' => $request['ip'],
            'type' => $type,
            'create_time'=>time()
        ];
        GameOperateLog::query()->insert($insert);

    }

    /**
     * 返回 400 Bad Request 响应
     *
     * @param string $message
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function error(string $message = '')
    {
        return response()->json(['message' => $message], 400);
    }
}
